package com.frogs.ucenter.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.frogs.framework.base.web.BaseController3;
import com.frogs.framework.util.DWZResponse;
import com.frogs.framework.util.PageList;
import com.frogs.framework.util.RenderUtil;
import com.frogs.framework.util.StringUtil;
import com.frogs.framework.web.HttpRequestInfo;
import com.frogs.system.util.ShippingAddressStatusEnum;
import com.frogs.ucenter.entity.UserShippingAddress;
import com.frogs.ucenter.entity.UserShippingAddressWithAreaCode;
import com.frogs.ucenter.service.AreaInfoService;
import com.frogs.ucenter.service.UserShippingAddressService;

@Controller
@RequestMapping("/admin/address")
public class UserShippingAddressController extends BaseController3 {
    private static final String ROOT_VIEW = "admin/userShippingAddress";

    private static final String HOME_VIEW = "admin/userShippingAddress/home";

    private static final String EDIT_VIEW = "admin/userShippingAddress/edit";

    @Resource
    private UserShippingAddressService userShippingAddressService;
    @Resource
    private AreaInfoService areaInfoService;

    @RequestMapping()
    public String listUserShippingAddress(HttpServletRequest request, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        setRequestModelMap( request, model, true );
        String rel = reqInfo.getParameter("rel","");
        model.put("parentRel", rel);
        getPageList( model );
        return HOME_VIEW;
    }

    @RequestMapping(EDIT)
    public String createUserShippingAddress(HttpServletRequest request, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        setRequestModelMap( request, model, false );
        Integer id = reqInfo.getIntParameter( "id", -1 );
        
        if( id > 0 ){
        	UserShippingAddressWithAreaCode address = userShippingAddressService.getById( id );
        	String areaId = address.getAreaId();
        	if(!StringUtil.isEmpty(areaId)) {
        		address.setProvinceId(areaId.substring(0, 2));
        		address.setCityId(areaId.substring(0, 4));
        		address.setDistrictId(areaId);
        	}
            model.put( "address", address); 
        }
        return EDIT_VIEW;
    }

    @RequestMapping(ADD)
    public String saveUserShippingAddress(HttpServletRequest request, HttpServletResponse response, ModelMap model, UserShippingAddressWithAreaCode userShippingAddress) {
        setRequestModelMap(request, model, false);
        DWZResponse.Builder builder;
        try {
            userShippingAddressService.insert( userShippingAddress ); 
            builder = DWZResponse.getSucessBuilder("success");
        } catch (Exception e) {
            builder = DWZResponse.getFailBuilder("fail" + e.getMessage());
        }
        RenderUtil.renderHtml(builder.build().toString(), response);
        return null;
    }

    @RequestMapping(SHOW)
    public String showUserShippingAddress(HttpServletRequest request, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        Integer id = reqInfo.getIntParameter( "id", -1 );
        if( id > 0 ){
            model.addAttribute( userShippingAddressService.getById( id ) ); 
        }
        setRequestModelMap(request, model);
        return null;
    }

    @RequestMapping(UPDATE)
    public String updateUserShippingAddress(HttpServletRequest request, HttpServletResponse response, ModelMap model, UserShippingAddress userShippingAddressNew) throws Exception {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        Integer id = reqInfo.getIntParameter( "id", -1 );
        if( id > 0 ){
            DWZResponse.Builder builder;
            try {
                UserShippingAddressWithAreaCode userShippingAddress = userShippingAddressService.getById( id ); 
                String name = reqInfo.getParameter("name");
                userShippingAddress.setName(name);
                
                String contacts = reqInfo.getParameter("contacts","");
                if(!StringUtil.isEmpty(contacts)) {
                	userShippingAddress.setContacts(contacts);
                }
                
                String contactNumber = reqInfo.getParameter("contactNumber", "");
                if(!StringUtil.isEmpty(contactNumber)) {
                	userShippingAddress.setContactNumber(contactNumber);
                }
                
                if(!StringUtil.isEmpty(userShippingAddressNew.getAreaId())) {
                	userShippingAddress.setAreaId(userShippingAddressNew.getAreaId());
                } else {
                	throw new Exception("请选择地区");
                }
                
                if(!StringUtil.isEmpty(userShippingAddressNew.getProvince())) {
                	userShippingAddress.setProvince(userShippingAddressNew.getProvince());
                }
                if(!StringUtil.isEmpty(userShippingAddressNew.getCity())) {
                	userShippingAddress.setCity(userShippingAddressNew.getCity());
                }
                if(!StringUtil.isEmpty(userShippingAddressNew.getDistrict())) {
                	userShippingAddress.setDistrict(userShippingAddressNew.getDistrict());
                }
                if(!StringUtil.isEmpty(userShippingAddressNew.getStreet())) {
                	userShippingAddress.setStreet(userShippingAddressNew.getStreet());
                	String fullAddress = userShippingAddress.getProvince() + userShippingAddress.getCity() + userShippingAddress.getDistrict() + userShippingAddress.getStreet();
                	userShippingAddress.setFullAddress(fullAddress);
                }
                
                userShippingAddress.setPostcode(userShippingAddressNew.getPostcode());
                
                userShippingAddressService.update( userShippingAddress ); 
                model.clear();
                builder = DWZResponse.getSucessBuilder("操作成功");
                builder.navTabId(String.valueOf(model.get("parentRel")));
                builder.callbackType("closeCurrent");
                builder.forwardUrl("/admin/user/accountInfo");
            } catch (Exception e) {
                builder = DWZResponse.getFailBuilder("fail" + e.getMessage());
            }
            RenderUtil.renderHtml(builder.build().toString(), response);
        }
        return null;
    }

    @RequestMapping(AUDIT)
    public String auditUserShippingAddress(HttpServletRequest request, HttpServletResponse response, ModelMap model, String inIdList) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        Integer status = reqInfo.getIntParameter( "status", -1 );
        
        setRequestModelMap(request, model);
        DWZResponse.Builder builder;
        try {
            String[] idArray = inIdList.split(",");
            model.put("inIdList", idArray);
            userShippingAddressService.update( model ); 
            builder = DWZResponse.getSucessBuilder("ɾ����Ϣ�ɹ�");
        } catch (Exception e) {
            builder = DWZResponse.getFailBuilder("ɾ����Ϣʧ�ܣ�" + e.getMessage());
        }
        RenderUtil.renderHtml(builder.build().toString(), response);
        model.clear();
        return null;
    }

    @RequestMapping(DELETE)
    public String deleteUserShippingAddress(HttpServletRequest request, HttpServletResponse response, ModelMap model, String ids) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        setRequestModelMap(request, model);
        DWZResponse.Builder builder;
        try {
            String[] idArray = ids.split(",");
            model.put("inIdList", idArray);
            model.put("status", ShippingAddressStatusEnum.STATUS_DELETE.getId());
            userShippingAddressService.update( model ); 
            model.clear();
            builder = DWZResponse.getSucessBuilder("操作成功");
        } catch (Exception e) {
            builder = DWZResponse.getFailBuilder("操作失败" + e.getMessage());
        }
        RenderUtil.renderHtml(builder.build().toString(), response);
        model.clear();
        return null;
    }
    
    @RequestMapping("/restore")
    public String restoreUserShippingAddress(HttpServletRequest request, HttpServletResponse response, ModelMap model, String ids) {
    	HttpRequestInfo reqInfo = new HttpRequestInfo(request);
    	setRequestModelMap(request, model);
    	DWZResponse.Builder builder;
    	try {
    		String[] idArray = ids.split(",");
    		model.put("inIdList", idArray);
    		model.put("status", ShippingAddressStatusEnum.STATUS_NOT_DELETE.getId());
    		userShippingAddressService.update( model );
    		builder = DWZResponse.getSucessBuilder("操作成功");
    	} catch (Exception e) {
    		builder = DWZResponse.getFailBuilder("操作失败" + e.getMessage());
    	}
    	RenderUtil.renderHtml(builder.build().toString(), response);
    	model.clear();
    	return null;
    }

    private PageList<UserShippingAddressWithAreaCode> getPageList(ModelMap model) {
        PageList<UserShippingAddressWithAreaCode> userShippingAddressList = userShippingAddressService.getList( model, Integer.parseInt(model.get("pageNum").toString()), Integer.parseInt(model.get("numPerPage").toString()) ); 
        model.put("userShippingAddressList",userShippingAddressList);
        model.put("pageTurn",userShippingAddressList.getPageTurn());
        return userShippingAddressList;
    }
}